package com.zj.healthmanage.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zj.healthmanage.entity.PageResult;
import com.zj.healthmanage.entity.QueryPageBean;
import com.zj.healthmanage.mapper.MemberMapper;
import com.zj.healthmanage.mapper.OrdersettingMapper;
import com.zj.healthmanage.pojo.Member;
import com.zj.healthmanage.utils.DateUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.zj.healthmanage.mapper.OrderlistMapper;
import com.zj.healthmanage.pojo.Orderlist;
import com.zj.healthmanage.service.OrderlistService;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderlistServiceImpl implements OrderlistService{

    @Resource
    private OrderlistMapper orderlistMapper;

    @Resource
    private OrdersettingMapper ordersettingMapper;

    @Resource
    private MemberMapper memberMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return orderlistMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Orderlist record) {
        return orderlistMapper.insert(record);
    }

    @Override
    public int insertSelective(Orderlist record) {
        return orderlistMapper.insertSelective(record);
    }

    @Override
    public Orderlist selectByPrimaryKey(Integer id) {
        return orderlistMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Orderlist record) {
        return orderlistMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Orderlist record) {
        return orderlistMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo queryOrderListByPage(Map queryPage) {
        //获取queryPage中的queryString的各部分信息
        Map queryString = (Map) queryPage.get("queryString");

        String valueNumber = (String) queryString.get("valueNumber");
        if (valueNumber.length() == 0){
            valueNumber = null;
        }
        String valueType = (String) queryString.get("valueType");
        if (valueType.length() == 0){
            valueType = null;
        }
        String valueStatus = (String) queryString.get("valueStatus");
        if (valueStatus.length() == 0){
            valueStatus = null;
        }
        List<String> valueDate = (List<String>) queryString.get("valueDate");

        String startTime = null;
        String endTime = null;
        if (valueDate != null && valueDate.size() != 0){
            startTime = valueDate.get(0).split("T")[0];
            endTime = valueDate.get(1).split("T")[0];
        }
        Page<Map> orderMap = PageHelper.startPage((Integer) queryPage.get("currentPage"), (Integer)queryPage.get("pageSize"));
        orderlistMapper.queryOrderListByPage(startTime,endTime,valueType,valueStatus,valueNumber);
        return new PageInfo(orderMap);
    }

    @Override
    public void addOrderList(Map OrderList, String[] setmealIds) throws Exception {
        String telephone = (String) OrderList.get("phoneNumber");
        //调用dao层方法将预约信息存入数据库中
        //该用户的预约时间是否设置了可预约人数
        //查询用户预约日期是否存在
        String orderDate = (String) OrderList.get("orderDate");
        String time = orderDate.split("T")[0];
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtils.parseString2Date(time));
        String date_2 = calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+(calendar.get(Calendar.DATE)+1);

        int countOrderDate = ordersettingMapper.queryOrderSettingByDate(date_2);
        if (countOrderDate == 0){
            new RuntimeException("当前日期不可预约");
        }
        //已预约量是否已经大于或等于可预约人数
        Map<String,Integer> result = ordersettingMapper.queryReservations(date_2);
        if (result.get("number") <= result.get("reservations")){
            new RuntimeException("当前日期预约人数已满");
        }

        //判断用户是否存在重复预约-->在此处还需判断，该用户是否存在 存在/不存在-->查询用户-->自动注册会员
        //member_id orderDate setmeal_id
        Member member = memberMapper.queryMemberByTelephone(telephone);
        if (member != null) {
            Integer memberId = member.getId();
            Date date = DateUtils.parseString2Date(date_2);
            for (String setmealId : setmealIds) {
                List<Orderlist> orderlists = orderlistMapper.queryOrderListByMemberId(memberId, date, Integer.parseInt(setmealId));
                if (orderlists != null && orderlists.size() != 0){
                    new RuntimeException("所选套餐中存在与之前预约重复的套餐，请查看后更换");
                }
            }

        }else{
            member = new Member();
            member.setName((String) OrderList.get("name"));
            member.setPhonenumber(telephone);
            member.setRegtime(new Date());
            //插入当前预约用户信息到会员表中
            memberMapper.insertSelective(member);
        }
        //未预约存入预约信息 更新预约量信息
        for (String setmealId : setmealIds) {
            Orderlist orderlist = new Orderlist();
            orderlist.setMemberId(member.getId());
            orderlist.setOrderdate(DateUtils.parseString2Date(date_2));
            orderlist.setOrdertype("电话预约");
            orderlist.setOrderstatus("未到诊");
            orderlist.setSetmealId(Integer.parseInt(setmealId));
            orderlistMapper.insertSelective(orderlist);
            //预约成功 orderSetting 表reservations +1
            ordersettingMapper.editReservations(DateUtils.parseString2Date(date_2));
        }
    }

    @Override
    public List<Integer> querySetMealIdsById(Integer id) {
        return ordersettingMapper.querySetMealIdsById(id);
    }


    @Override
    public void editOrderList(Map orderList, String[] setmealIdList) throws Exception {
        String orderDate = (String) orderList.get("orderDate");
        String date = orderDate.split("T")[0];

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtils.parseString2Date(date));
        String date_2 = calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+(calendar.get(Calendar.DATE)+1);

        int countOrderDate = ordersettingMapper.queryOrderSettingByDate(date_2);
        if (countOrderDate == 0){
            new RuntimeException("当前日期不可预约");
        }

        //已预约量是否已经大于或等于可预约人数
        //String  setmealId = (String) map.get("setmealId");
        Map<String,Integer> result = ordersettingMapper.queryReservations(date_2);
        if (result.get("number") <= result.get("reservations")){
            new RuntimeException("当前日期预约人数已满");
        }

        Orderlist orderlist = orderlistMapper.queryOrderListById((Integer) orderList.get("id"));

        Date beforeOrderDate = orderlist.getOrderdate();
        ordersettingMapper.editNumber(beforeOrderDate);

        orderlist.setOrderdate(DateUtils.parseString2Date(date_2));
        orderlist.setOrdertype((String) orderList.get("orderType"));
        orderlist.setOrderstatus((String) orderList.get("orderStatus"));
        orderlist.setSetmealId(Integer.parseInt(setmealIdList[0]));

        ordersettingMapper.editReservations(DateUtils.parseString2Date(orderDate));
        orderlistMapper.updateByPrimaryKeySelective(orderlist);
    }

    @Override
    public void editOrderStatus(Integer id, String orderStatus) {
        Orderlist orderlist = orderlistMapper.queryOrderListById(id);
        orderlist.setOrderstatus(orderStatus);
        orderlistMapper.updateByPrimaryKeySelective(orderlist);

    }

}
